package com.amarsoft.apot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //Swagger实例Bean是Docket,配置docket以配置Swagger具体参数
    @Bean
    public Docket docket1(Environment environment) {

        //设置要显示swagger的环境
        Profiles of= Profiles.of("dev","test","prod");
        // 判断当前是否处于该环境
        // 通过enable()接收此参数判断是否要显示
        boolean b = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("第一个")
                .enable(b)//配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select()//通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.amarsoft.apot.controller"))//配置如何通过path过滤,即这里只扫描请求以/hello开头的接口
                .paths(PathSelectors.ant("/hello/**"))
                .build();
    }

    //分组+配置多个Docket对象，企业中每个人负责每个人的docket
    @Bean
    public Docket docket2(Environment environment) {

        //设置要显示swagger的环境
        Profiles of= Profiles.of("dev","test","prod");
        // 判断当前是否处于该环境
        // 通过enable()接收此参数判断是否要显示
        boolean b = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("第二个")
                .enable(b);//配置是否启用Swagger，如果是false，在浏览器将无法访问
    }

    //配置文档信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("项目标题","http://xxx.xxx.com/联系人访问链接","1910961676@qq.com");
        return new ApiInfo(
                "Swagger学习", //标题
                "学习演示如何配置Swagger", //描述
                "v1.0", //版本
                "http://terms.service.url/组织链接", //组织链接
                contact, //联系人信息
                "Apach 2.0 许可", //许可
                "许可链接", //许可链接
                new ArrayList<>() //扩展
        );
    }
}
